﻿using System;
using System.Collections;
using System.Drawing;
using System.Text;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using AjaxControlToolkit;
using DGP.Framework.Web.UI;
using Model;

public partial class Article : BasePage
{
    private Articles current;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!string.IsNullOrEmpty(Request.QueryString["ID"]))
        {
            pnlArticle.Visible = true;
            current = Entities.GetArticleDetails(Convert.ToInt32(Request.QueryString["ID"]));
            litNav.Text = LoadNavigation(current);

            litTitle.Text = current.Title;
            litAuthor.Text = current.Author.FirstName + " " + current.Author.LastName;
            litDatePosted.Text = current.DatePosted.Value.ToShortDateString();
            DGPContentEditor1.Text = current.Content;
            litContent.Text = current.Content;
            if (!string.IsNullOrEmpty(current.ImagePath))
            {
                var img = new Bitmap(Utils.GetApplicationSetting(Constants.IMAGES_ARTICLES_PHYSICAL_PATH) + current.ImagePath);
                imgArticle.ImageUrl = Utils.GetApplicationSetting(Constants.IMAGES_ARTICLES_URL_PREFIX) + current.ImagePath;
                imgArticle.AlternateText = current.Title;

                if (img.Width > img.Height)
                    imgArticle.Width = new Unit(250);
                else
                    imgArticle.Height = new Unit(250);
            }
            else
                imgArticle.Visible = false;

            imgArticle.AlternateText = current.Title;
            Rating1.CurrentRating = Convert.ToInt32(current.Rank);

            litVotes.Text = string.Format(litVotes.Text, current.Votes);

            repTags.DataSource = current.MetaKeys.Split(',');
            repTags.DataBind();

            // set title and meta tags;
            Title = current.Title;
            Meta_Description = current.MetaDesc;
            Meta_Keywords = current.MetaKeys;
        }

        if (Master != null)
        {
            var topLiItem = (HtmlGenericControl)Master.FindControl("liArticles");
            topLiItem.Attributes.Add("class", "current");
        }
    }

    protected string LoadNavigation(Articles article)
    {
        var sb = new StringBuilder();
        sb.Append(@"<a href=""default.aspx"">Prima pagina</a>");

        Categories parentCategory = article.Category;
        var nav = new ArrayList { parentCategory };
        while (parentCategory.Parent.CategoryID != 0)
        {
            try
            {
                parentCategory = parentCategory.Parent;
                nav.Add(parentCategory);
            }
            catch
            {
                break;
            }
        }
        for (int i = nav.Count - 1; i >= 0; i--)
        {
            var x = (Categories)nav[i];
            sb.Append(@" » <a href=""" + Utils.BuildURL(PageType.Categorii, x.CategoryID, x.Title) + @""">" + x.Title + @"</a>");
        }
        sb.Append(@" » " + article.Title);
        return sb.ToString();
    }

    protected void Rating_Changed(object sender, RatingEventArgs arg)
    {
        current = Entities.RateArticle(current.ID, (Convert.ToInt32(arg.Value)));
        mdlPopup.Hide();
    }

    protected void btnClose_Click(object sender, EventArgs e)
    {
        litVotes.Text = string.Format(litVotes.Text, current.Votes);
        Rating1.CurrentRating = Convert.ToInt32(current.Rank);
        UpdatePanel1.Update();
    }
}